import pygame

class MainClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
        self.frame = 0
        self.images = {'width': 115, 'height': 110}
        self.frames = {'per_row': 5, 'frame_width': 120, 'frame_height': 160, 'real_frame_width': 115, 'cut_top': 50}
        self.delay = 7
        self.pause = 0
        self.center_vals = {'x':80, 'y':360}
        self.loadImages()
        self.image = self.imgList[0]
        self.rect = self.image.get_rect()
        
    def loadImages(self):
        imgMaster = pygame.image.load("cat_with_legs.png").convert_alpha()
        
        self.imgList = []
        self.imgList_walk = []
        
        imgSize = (self.images['width'], self.images['height'])
        for col in (0,1):
            for i in range(self.frames['per_row']):
                tmpImg = pygame.Surface(imgSize,pygame.SRCALPHA)
                tmpImg.blit(imgMaster, (0, 0), (self.getOffset(i, col), imgSize))
                #transColor = tmpImg.get_at((1, 1))
                #tmpImg.set_colorkey(transColor)
                if col == 0:
                    self.imgList.append(tmpImg)
                elif col == 1:
                    self.imgList_walk.append(tmpImg)
    
    def getOffset(self, i, col=0):
        offset1 = i*self.frames['frame_width']
        offset2 = self.frames['cut_top']+col*self.frames['frame_height']
        return (offset1, offset2)
    
    def update(self, x=0,y=0, action = 0):
        self.pause += 1
        if self.pause >= self.delay:
            self.pause = 0
            self.frame += 1
            if self.frame >= len(self.imgList):
                self.frame = 0
        
        if action == 0:
            self.image = self.imgList[self.frame]
        else:
            self.image = self.imgList_walk[self.frame]
        self.rect = self.image.get_rect()
        self.center_vals['x'] += x;
        self.center_vals['y'] += y;
        self.rect.center = (self.center_vals['x'], self.center_vals['y'])
